﻿using Lis.IBLL;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lis.Web.Controllers
{
    public class UserInfoController : Controller
    {
        // GET: UserInfo
        //UserInfoService userInfoService = new UserInfoService();

        //Spring.Net--使用注入的方式

        public IUserInfoService UserInfoService { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        #region 01，获取所有用户信息操作+JsonResult ShowAllUser()
        /// <summary>
        /// 获取所有用户信息操作
        /// </summary>
        /// <returns></returns>
        public JsonResult ShowAllUser()
        {
            int pageInde =Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize =Request["rows"] == null ? 1 : int.Parse(Request["rows"]);
            int totalCount;
            var userList = UserInfoService.GetPageEntities<DateTime?>(pageSize, pageInde, out totalCount, true, u => u.YXRQ >= DateTime.Now, u => u.BZRQ).ToList();
            var temp = from u in userList
                       select new
                       {
                           Id = u.DZTM,
                           ShowName = u.XM,
                           UserType=u.DZLX,
                           OpenDate=u.BZRQ,
                           EndDate=u.YXRQ
                       };
            //客户端就是遍历rows  jq easy-ui就是遍历rows
            return Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 02，添加用户操作+JsonResult AddUser(UserInfo userInfo)
        /// <summary>
        /// 添加用户操作
        /// </summary>
        /// <param name="userInfo">用户对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddUser(UserInfo userInfo)
        {
            if (ModelState.IsValid)
            {
                userInfo.BZRQ = DateTime.Now;
                userInfo.MM = "1234";
                userInfo.YJ = "0";
                userInfo.XB = userInfo.XB == "0" ? "男" : "女";
                if (userInfo.DZLX == "0")//本科生
                {
                    userInfo.DZLX = "本科生";
                }
                else if (userInfo.DZLX == "1")//博士生
                {
                    userInfo.DZLX = "博士生";
                }
                else if (userInfo.DZLX == "2")//教师
                {
                    userInfo.DZLX = "教师";
                }
                else if (userInfo.DZLX == "3")//学生
                {
                    userInfo.DZLX = "专升本";
                }
                else if (userInfo.DZLX == "4")//研究生
                {
                    userInfo.DZLX = "专升本";
                }
                else//专升本
                {
                    userInfo.DZLX = "专升本";
                }
                userInfo.ZJLX = "借阅证";
                userInfo.DWMC = "安徽农业大学";
            }
            var exist= UserInfoService.Add(userInfo);
            if (exist)
            {
                return Content("ok");
            }
            else
            {
                return Content("ok");
            }
            
        }
        #endregion

        #region 03，用户编辑
        /// <summary>
        /// 获取要编辑的用户的信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public ActionResult EditUser(string id)
        {
            UserInfo model = UserInfoService.GetEntityById(id);
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditUser(UserInfo model)
        {
            var userModel = UserInfoService.GetEntityById(model.DZTM);
            userModel.XM = model.XM;
            userModel.DZLX = model.DZLX;
            userModel.MM = model.MM;
            userModel.YXRQ = model.YXRQ;
            var exist = UserInfoService.UpDate(userModel);
            if (exist)
            {
                return Content("ok");
            }
            else
            {
                return Content("flase");
            }
        } 
        #endregion


    }
}